<template>
  <div class="app-container">
    <el-card shadow="never" class="filter-bar" >
      <el-form :inline="true" :model="form" size="mini">
        <el-form-item label="">
          <el-input v-model="form.keyword" placeholder="昵称或者手机号"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card shadow="never">
      <el-table
        v-loading="loading"
        :data="list"
        style="width: 100%">
        <el-table-column
          prop="name"
          label="昵称">
        </el-table-column>
        <el-table-column
          prop="telphone"
          label="手机号">
        </el-table-column>
        <el-table-column
          prop="name"
          label="头像">
          <template slot-scope="scope">
            <img :src="scope.row.avatar" width="50px" alt="">
          </template>
        </el-table-column>
        <el-table-column
          prop="created_at"
          label="状态">
          <template slot-scope="scope">
            <el-tag
              type="success"
              close-transition>正常</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="created_at"
          label="注册日期">
        </el-table-column>
      </el-table>

      <div class="pagination-container">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageMeta.page"
          :page-sizes="[15, 25, 50, 100]"
          :page-size="pageMeta.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pageMeta.total">
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import { fetchList, show } from '@/api/user'

export default {
  name: 'store',
  data() {
    return {
      list: [],
      pageMeta: {
        total: 0,
        size: 15,
        page: 1
      },
      form: {
        keyword: ''
      },
      loading: true
    }
  },
  created: function() {
    this.getUserList()
  },
  methods: {
    // 获取用户列表
    getUserList() {
      this.loading = true
      fetchList({
        page: this.pageMeta.page,
        size: this.pageMeta.size,
        keyword: this.form.keyword
      }).then(res => {
        const data = res.data
        this.list = data.data
        this.pageMeta.total = data.total
        this.loading = false
      }).catch(err => {
        this.loading = false
        console.log(err)
      })
    },
    showUser() {
      show()
    },
    // 重设每页显示数量
    handleSizeChange(val) {
      this.pageMeta.size = val
      this.getUserList()
    },
    // 翻页
    handleCurrentChange(val) {
      this.pageMeta.page = val
      this.getUserList()
    },
    onSubmit() {
      this.pageMeta.page = 1
      this.pageMeta.size = 15
      this.getUserList()
    }
  }
}
</script>